public without sharing class WorkOrderTeamHandler implements Triggers.Handler 
{
    List<WorkOrderTeam__c> wotList = new List<WorkOrderTeam__c>();
    Map<Id,String> errorMap = new Map<Id,String>();
    Set<Id> caseIdSet = new Set<Id>();
    List<Caseshare> delList = new List<Caseshare>();    
    public void handle() {
        if(Trigger.isAfter){
            if(Trigger.isDelete)
            {
                wotList.addAll((List<WorkOrderTeam__c>)Trigger.old);
                if(!wotList.isEmpty()){
                     delCaseShare();
                }

            }else if(Trigger.isInsert){
                wotList.addAll((List<WorkOrderTeam__c>)Trigger.new);            
                System.debug('@wotList: '+wotList);
                if (!wotList.isEmpty()) {
                    addCaseShare();
                }                
            }else{
                wotList.addAll((List<WorkOrderTeam__c>)Trigger.old);//先删除旧值
                if(!wotList.isEmpty()){
                     delCaseShare();
                }
                wotList.clear();
                wotList.addAll((List<WorkOrderTeam__c>)Trigger.new);    //再插入新值        
                System.debug('@wotList: '+wotList);
                if (!wotList.isEmpty()) {
                    addCaseShare();
                }    

            }
        }
    }

    public void delCaseShare(){
            System.debug('delete share: '+wotList);
            for (WorkOrderTeam__c wot : wotList ) {
                caseIdSet.add(wot.CaseId__c);
            }
            List<Caseshare> caseshareList = [select Id,UserOrGroupId,CaseId,Case.OwnerId from Caseshare where CaseId in: caseIdSet];
            System.debug(caseshareList);
            for (WorkOrderTeam__c wot : wotList ){
                for(Caseshare c : caseshareList){
                    String acsId = (String)c.CaseId;
                    //String auId = (String)c.UserOrGroupId;
                    System.debug(wot.CaseId__c+'   '+acsId.left(15)+'   '+wot.User__c+'   '+c.UserOrGroupId);
                    if(wot.CaseId__c == acsId.left(15) && wot.User__c == c.UserOrGroupId && wot.User__c != c.Case.OwnerId){
                        System.debug('hao: '+c);
                        delList.add(c);
                        System.debug('hao: '+delList);
                    }
                }
            }
            if(!delList.isEmpty()){
                system.debug(delList);
                delete delList;
            }
    }

    public void addCaseShare(){
        List<Caseshare> shareList = new List<Caseshare>();
        //记录下CaseID
        List<String> cIDS = new List<String>();
        for (WorkOrderTeam__c wot : wotList ) {
            if(!String.isBlank(wot.CaseId__c)) {
                cIDS.add(wot.CaseId__c);
            }
        }
        Map<String,String> mapAccount = new Map<String,String>();
        //取出个案对应的客户名
        if(cIDS.size() > 0){
            List<Case> tmpList = [SELECT Id,Account.Name FROM Case WHERE Id IN : cIDS];
            if(tmpList != null && tmpList.size() > 0){
                for(Case c : tmpList){
                    if(!mapAccount.containsKey(c.Id)){
                        mapAccount.put(c.Id, c.Account.Name);
                    }
                }
            }
        }
        Map<String,Map<String,Id>> chatters = new Map<String,Map<String ,Id>>();
        for (WorkOrderTeam__c wot : wotList ) {
            if(!String.isBlank(wot.CaseId__c)) {
                if(!String.isBlank(wot.CaseOwnerId__c))
                {
                    if(wot.CaseOwnerId__c == wot.User__c){
                        System.debug('@wot.Id: '+wot.Id+'该用户是所选工单个案的所有人！');
                    }else{
                        Caseshare ashare = new Caseshare();
                        ashare.CaseAccessLevel = 'Edit';
                        ashare.CaseId = wot.CaseId__c;
                        ashare.UserOrGroupId = wot.User__c;
                        shareList.add(ashare);

                        Map<String,Id>usMp = new Map<String,Id>();

                        usMp.put('woId',wot.WorkOrder__c);

                        usMp.put('casId', wot.CaseId__c);

                        usMp.put('uId', wot.User__c);

                        chatters.put(wot.User__c+'-'+wot.WorkOrder__c,usMp);

                        //postChatter(wot.User__c,wot.WorkOrder__c,mapAccount.get( Id.valueOf(wot.CaseId__c)));
                   }
                }
            }
        }
        System.debug('@shareList: '+shareList);
        if(!shareList.isEmpty()){
            insert shareList;
        }

        if(!chatters.isEmpty()){
            for(Map<String,Id> usMp : chatters.values()){
                
                postChatter(usMp.get('uId'),usMp.get('woId'),mapAccount.get( Id.valueOf(usMp.get('casId'))));

            }
        }
    }

    public static void  postChatter(Id aid,Id wid,String accName){
        //ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), wid, ConnectApi.FeedElementType.FeedItem, '@ \n请支援工单https://cs31.salesforce.com/'+wid+'!');
        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
        mentionSegmentInput.id = aid;
        messageBodyInput.messageSegments.add(mentionSegmentInput);
        textSegmentInput.text = ',请支援该工单【客户名：' + accName +'】！';
        messageBodyInput.messageSegments.add(textSegmentInput);
        feedItemInput.body = messageBodyInput;
        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
        feedItemInput.subjectId = wid;
        ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);



    }

    
}